<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="/js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/bootstrap.min.css" />
    <link rel="stylesheet" href="/css/my-css.css" />
    <script type="text/javascript" src="/js/my-js.js" ></script>
    <script type="text/javascript" src="/js/vue.min.js" ></script>
    <script type="text/javascript" src="/js/jquery.cookie.js" ></script>
    <title>Java学习网</title>
    <style type="text/css">
        nav {
            font-size: 16px;
            font-family: "微软雅黑";
        }
        .div-content {
            margin-top: 10px;
            margin-bottom: 30px;
            padding: 0 20px 20px 20px;
        }
    </style>
</head>
<body>
    <div id="header"></div>
    <script type="text/javascript">moralLoad("#header", "/html/header.html")</script>
    <div class="container">
        <div class="row">
            <div class="col-xs-offset-1 col-xs-10 panel panel-default div-content">
                <h3><b>添加题目</b></h3>
                <hr />
                <form class="form-horizontal" id="addQuestion">
                    <div class="form-group">
                        <label for="questionContent" class="col-xs-2 control-label">题干</label>
                        <div class="col-xs-7">
                            <textarea name="questionContent" id="questionContent" rows="5" class="form-control" style="resize: none;" required="required"></textarea>
                        </div>
                    </div>
                    <div id="options">
                        <div class="form-group" id="option">
                            <label class="col-xs-2 control-label">选项
                                <br /><input type="checkbox" name="isCorrect"/>正确选项
                            </label>
                            <div class="col-xs-6">
                                <textarea name="choiceContent" rows="3" class="form-control" style="resize: none;" required="required"></textarea>
                            </div><br />
                            <button type="button" class="btn btn-danger" onclick="deleteOption.call(this)">删除</button>
                        </div>
                    </div>
                    <div class="col-xs-offset-2 col-xs-4">
                        <button type="button" class="btn btn-success" id="addOption">
                            <span class="glyphicon glyphicon-plus"></span>
                        </button>
                    </div><br />
                    <div style="margin: 20px 0 20px 0; text-align: center;">
                        <p id="message" class="text-danger" style="visibility: hidden;">!</p>
                    </div>
                    <div class="col-xs-offset-2 col-xs-7">
                        <button type="submit" class="pull-left col-xs-4 btn btn-warning">添加</button>
                        <button type="button" class="pull-right col-xs-4 btn btn-primary" onClick="location.href='/login.html'">返回</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript">
    var optionDiv = $("#option")[0].outerHTML;
    addOption();addOption();addOption();
    $("#addOption").click(function() {addOption()});
    function deleteOption() {
        $(this).parent().remove();
    };
    function addOption() {
        $("#options").append(optionDiv);
    }
    var $addForm = $("#addQuestion");
    $addForm.submit(function() {
        var questionContent = $("#questionContent").val().trim();
        var choicesContent = new Array();
        var isCorrects = new Array();
        var correctCount = 0;
        var isChoiceContentHasEmpty = false;
        if (questionContent === undefined || questionContent === "") {
            showMessage("题干不能为空！");
            return false;
        }
        $("textarea[name='choiceContent']").each(function() {
            var content = $(this).val().trim();
            if (content === undefined || content === "") {
                isChoiceContentHasEmpty = true;
            }
            choicesContent.push(content);
        });
        if (isChoiceContentHasEmpty === true) {
            showMessage("选项文本不能为空！");
            return false;
        }
        if (choicesContent.length < 2) {
        	showMessage("至少要有两个选项！");
        	return false;
        }
        $("input[name='isCorrect']").each(function() {
            isCorrects.push(this.checked);
            correctCount += this.checked === true ? 1 : 0;
        });
        if (correctCount < 1) {
            showMessage("至少要有一个正确选项！");
            return false;
        }
        var data = {
            questionContent: questionContent,
            choicesContent: choicesContent,
            corrects: isCorrects
        };
        myAjax({
        	type: 'post',
        	url: document.location.pathname.split("/addQuestion")[0],
        	data: JSON.stringify(data),
        	contentType: "application/json",
        	success: function() {
        		showMessage("添加成功！");
        		$addForm[0].reset();
        	}
        });
        return false;
    });
</script>
</html>